package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.CaseDispatch;
import com.battle.pojo.enums.CaseDispatchTypeEnum;
import com.battle.pojo.param.caseDispatch.*;
import com.battle.pojo.vo.caseDispatch.*;
import com.battle.pojo.dto.caseDispatch.*;
import com.battle.service.repository.in.CaseDispatchRepository;
import com.battle.service.service.in.CaseDispatchService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 【描 述】：调派情况服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class CaseDispatchServiceImpl implements CaseDispatchService {

    private final CaseDispatchRepository caseDispatchRepository;

    public CaseDispatchServiceImpl(CaseDispatchRepository caseDispatchRepository) {
        this.caseDispatchRepository = caseDispatchRepository;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createCaseDispatch(Boolean isTure,List<CreateCaseDispatchParam> param, CaseDispatchTypeEnum type, Long caseId, Long sirensId) {
        CaseDispatch po = new CaseDispatch();
        po.setIsDelete(false);
        po.setCaseId(caseId);
        po.setType(type);
        List<CaseDispatch> deleteList = caseDispatchRepository.getListByEntity(po);

        if (CollectionUtil.isNotEmpty(deleteList)) {
            List<Long> ids = deleteList.stream().map(CaseDispatch::getId).distinct().collect(Collectors.toList());
            if (isTure){
                caseDispatchRepository.trueDeletionByIds(ids);
            }else{
                caseDispatchRepository.removeBatchByIds(ids);
            }
        }

        if (param!=null){
            if (CollectionUtil.isNotEmpty(param)){
                List<CaseDispatch> caseDispatchList=new ArrayList<>();
                for (CreateCaseDispatchParam dispatchParam : param) {
                    CaseDispatch caseDispatch=new CaseDispatch();
                    BeanUtils.copyProperties(dispatchParam,caseDispatch);
                    caseDispatch.setId(IdGenerator.SNOW.generate());
                    caseDispatch.setCaseId(caseId);
                    caseDispatch.setSirensId(sirensId);
                    caseDispatch.setType(type);
                    caseDispatchList.add(caseDispatch);
                }
                caseDispatchRepository.saveBatch(caseDispatchList);
            }
        }

    }

    @Override
    public List<CreateCaseDispatchParam> getCaseDispatchDownList(Long caseId, CaseDispatchTypeEnum type) {
        CaseDispatch po = new CaseDispatch();
        po.setIsDelete(false);
        po.setCaseId(caseId);
        po.setType(type);

        List<CaseDispatch> caseDispatchList = caseDispatchRepository.getListByEntity(po);

        List<CreateCaseDispatchParam> list = new ArrayList<>();
        for (CaseDispatch caseDispatch : caseDispatchList) {
            CreateCaseDispatchParam vo = new CreateCaseDispatchParam();
            BeanUtils.copyProperties(caseDispatch, vo);
            list.add(vo);
        }
        return list;
    }
}



